import i18n from '../../../i18n';
import { car_information } from './car_imformation';
import { ChangeType } from './changeType';
/**
 * 可购买的赛车
 */
const canbuy: string[] = [
  'FCX Mopeak SC-ONE 银白',
  'FCX NEO 9 Classic',
  'FCX Elado Apollo',
  'FCX New Era 2 Pro',
  'FCX Nexus C方程式 Sakura',
  'FCX Nexus C方程式赛车',
  'FCX Elado Apollo RX Star',
  'FCX Future Neo X 纯白',
  'FCX Elado 绿野奇迹',
  'FCX Future Neo X 纯白 PRO',
  'FCX Neo 8',
  'FCX Future Neo X 巅峰白',
  'FCX TECH-CX7000DRB',
  'FCX ETechsc Apollo 2吉吉喵',
  'FCX New Era 夏天蓝',
  'FCX ETechsc Apollo ice cream',
  'FCX New Era 2 super',
  'FCX ETechsc Apollo 2美术喵',
];
/**
 * 抽奖车信息
 */
const cancj = [
  {
    id: 'car_03',
    name: 'FCX Elado Apollo', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.56,
    jumpPower: 0.74,
    acceleration: 0.14,
    isDz: true,
    fuelType: 'electric',
  },
  {
    id: 'car_04',
    name: 'FCX New Era 2 Pro', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.59,
    jumpPower: 0.76,
    acceleration: 0.16,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_06',
    name: 'FCX Nexus C方程式赛车', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.65,
    jumpPower: 0.8,
    acceleration: 0.2,
    isDz: false,
    fuelType: 'electric',
  },
  {
    id: 'car_07',
    name: 'FCX Elado Apollo RX Star', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.68,
    jumpPower: 0.82,
    acceleration: 0.22,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_10',
    name: 'FCX Future Neo X 纯白 PRO', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.77,
    jumpPower: 0.88,
    acceleration: 0.28,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_12',
    name: 'FCX Future Neo X 巅峰白', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.83,
    jumpPower: 0.92,
    acceleration: 0.32,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_14',
    name: 'FCX ETechsc Apollo 2吉吉喵', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.89,
    jumpPower: 0.96,
    acceleration: 0.36,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_16',
    name: 'FCX ETechsc Apollo ice cream', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 1.95,
    jumpPower: 1.0,
    acceleration: 0.4,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_19',
    name: 'FCX Elado Apollo RX GM', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 2.04,
    jumpPower: 1.06,
    acceleration: 0.46,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_20',
    name: 'FCX Future Neo 3', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 2.07,
    jumpPower: 1.08,
    acceleration: 0.48,
    isDz: true,
    fuelType: 'gasoline',
  },
  {
    id: 'car_21',
    name: 'FCX Future Neo-sport', //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
    speed: 2.1,
    jumpPower: 1.1,
    acceleration: 0.5,
    isDz: true,
    fuelType: 'gasoline',
  },
];
/**
 * 生成随机数
 * @param min 最小值
 * @param max 最大值
 * @returns 生成的随机数
 */
const rand = (min: number, max: number) =>
  Math.round(Math.random() * (max - min) + min);
export class buyCar {
  constructor() {}
  /**
   * 获取赛车信息
   * @param carid 赛车id
   * @returns 赛车信息
   */
  static getCarInformation(carid: string) {
    return car_information[carid as keyof typeof car_information] ?? undefined;
  }
  /**
   * 更改玩家赛车名字
   * @param entity 玩家实体
   * @returns 无
   */
  static async changeCarName(entity: GamePlayerEntity) {
    const d1 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('engineer.name', { lng: entity.language }),
      content: i18n.t('engineer.select_car', { lng: entity.language }),
      options: entity.cars.map((x) => x.name),
    });
    if (d1) {
      const selectedCar = entity.cars[d1.index];
      const information = ChangeType.getCarInformation(selectedCar.id);
      const d2 = await entity.player.dialog<GameSelectDialogParams>({
        type: GameDialogType.SELECT,
        title: i18n.t('garage.title', { lng: entity.language }),
        content: `
            ${i18n.t('car_information.name', { name: information.name, lng: entity.language })},
            ${i18n.t('car_information.speed', { speed: information.speed, lng: entity.language })},
            ${i18n.t('car_information.jumpPower', { jumpPower: information.jumpPower, lng: entity.language })},
            ${i18n.t('car_information.acceleration', { acceleration: information.acceleration, lng: entity.language })},
            ${i18n.t('car_information.isDz', { isDz: information.isDz, lng: entity.language })},
            ${i18n.t('car_information.fuelType', { fuelType: information.fuelType, lng: entity.language })},
            `,
        options: [
          i18n.t('engineer.change_name', { lng: entity.language }),
          i18n.t('common.close', { lng: entity.language }),
        ],
      });
      if (!d2) {
        return;
      }
      switch (d2.value) {
        case i18n.t('engineer.change_name', { lng: entity.language }):
          const d3 = await entity.player.dialog<GameInputDialogParams>({
            type: GameDialogType.INPUT,
            title: i18n.t('engineer.name', { lng: entity.language }),
            content: i18n.t('engineer.change_name', { lng: entity.language }),
          });
          if (d3) {
            entity.cars[d1.index].name = d3 as string;
            await entity.player.dialog<GameTextDialogParams>({
              type: GameDialogType.TEXT,
              title: i18n.t('engineer.name', { lng: entity.language }),
              content: i18n.t('engineer.changename_success', {
                lng: entity.language,
              }),
            });
          }
          break;
      }
    }
  }
  /**
   * 购买赛车
   * @param entity 购买赛车的玩家
   * @returns 无
   */
  static async buycar(entity: GamePlayerEntity) {
    const d1 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('engineer.name', { lng: entity.language }),
      content: i18n.t('engineer.select_buy_car', { lng: entity.language }),
      options: canbuy,
    });
    if (!d1) {
      return;
    }
    const selectedCar =
      car_information[
        Object.keys(car_information)[d1.index] as keyof typeof car_information
      ];
    const d2 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('garage.title', { lng: entity.language }),
      content: `
          ${i18n.t('car_information.name', { name: selectedCar.name, lng: entity.language })},
          ${i18n.t('car_information.speed', { speed: selectedCar.speed, lng: entity.language })},
          ${i18n.t('car_information.jumpPower', { jumpPower: selectedCar.jumpPower, lng: entity.language })},
          ${i18n.t('car_information.acceleration', { acceleration: selectedCar.acceleration, lng: entity.language })},
          ${i18n.t('car_information.isDz', { isDz: selectedCar.isDz, lng: entity.language })},
          ${i18n.t('car_information.fuelType', { fuelType: selectedCar.fuelType, lng: entity.language })},
          `,
      options: [
        i18n.t('engineer.buy', { lng: entity.language }),
        i18n.t('common.close', { lng: entity.language }),
      ],
    });
    if (d2) {
      let value: number =
        selectedCar.jumpPower *
        selectedCar.speed *
        selectedCar.acceleration *
        5000 *
        (d1.index + 1);
      value = Number(value.toFixed(0));
      const d3 = await entity.player.dialog<GameSelectDialogParams>({
        type: GameDialogType.SELECT,
        title: i18n.t('engineer.name', { lng: entity.language }),
        content: i18n.t('engineer.need_pay', { lng: entity.language, value }),
        options: [
          i18n.t('common.yes', { lng: entity.language }),
          i18n.t('common.no', { lng: entity.language }),
        ],
      });
      if (d3) {
        switch (d3.value) {
          case i18n.t('common.yes', { lng: entity.language }):
            if (entity.coin >= value) {
              entity.player.directMessage(
                i18n.t('engineer.buy_success', { lng: entity.language })
              );
              entity.coin -= value;
              entity.cars.push(selectedCar);
            } else {
              entity.player.directMessage(
                i18n.t('engineer.buy_fail', { lng: entity.language })
              );
            }
            break;
        }
      }
    }
  }
  /**
   * 在玩家背包加入制定赛车
   * @param entity 玩家实体
   * @param id 赛车id
   */
  private static pushItem(entity: GamePlayerEntity, id: string) {
    entity.cars.push({
      id,
      name: car_information[id as keyof typeof car_information].name,
    });
    entity.player.directMessage(
      i18n.t('cj.gain', {
        lng: entity.language,
        string: car_information[id as keyof typeof car_information].name,
      })
    );
  }
  /**
   * 抽奖
   * @param entity 抽奖的玩家
   * @returns 无
   */
  cj(entity: GamePlayerEntity) {
    if (entity.coin < 2500) {
      entity.player.directMessage(
        i18n.t('engineer.buy_fail', { lng: entity.language })
      );
      return;
    }
    entity.coin -= 2500;
    const number = rand(1, 100);
    console.log(number);
    if (number < 10) {
      buyCar.pushItem(entity, 'car_03');
    } else if (number < 20) {
      buyCar.pushItem(entity, 'car_04');
    } else if (number < 29) {
      buyCar.pushItem(entity, 'car_06');
    } else if (number < 37) {
      buyCar.pushItem(entity, 'car_07');
    } else if (number < 44) {
      buyCar.pushItem(entity, 'car_10');
    } else if (number < 50) {
      buyCar.pushItem(entity, 'car_12');
    } else if (number < 56) {
      buyCar.pushItem(entity, 'car_14');
    } else if (number < 60) {
      buyCar.pushItem(entity, 'car_16');
    } else if (number < 63) {
      buyCar.pushItem(entity, 'car_19');
    } else if (number < 65) {
      buyCar.pushItem(entity, 'car_20');
    } else if (number < 66) {
      buyCar.pushItem(entity, 'car_21');
    } else {
      entity.coin += 1000;
      entity.player.directMessage(
        i18n.t('cj.gain', {
          lng: entity.language,
          string: i18n.t('cj.coin', { lng: entity.language, number: 1000 }),
        })
      );
    }
  }
  /**
   * 回收赛车
   * @param entity 回收赛车的玩家
   * @returns 无
   */
  static async recycle(entity: GamePlayerEntity) {
    const d1 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('recycle.title', { lng: entity.language }),
      content: i18n.t('recycle.content', { lng: entity.language }),
      options: entity.cars.map((value) => value.name),
    });
    if (d1) {
      const information = ChangeType.getCarInformation(
        entity.cars[d1.index].id
      );
      if (information.isDz) {
        return;
      }
      const index = Object.keys(car_information).indexOf(
        entity.cars[d1.index].id
      );
      const price = Math.round(
        information.jumpPower *
          information.speed *
          information.acceleration *
          500 *
          (index + 1)
      );
      const d2 = await entity.player.dialog<GameSelectDialogParams>({
        type: GameDialogType.SELECT,
        title: i18n.t('recycle.title', { lng: entity.language }),
        content: i18n.t('recycle.content_1', {
          lng: entity.language,
          string: d1.value,
          number: price,
        }),
        options: [
          i18n.t('common.yes', { lng: entity.language }),
          i18n.t('common.no', { lng: entity.language }),
        ],
      });
      if (d2) {
        if (d2.index === 0) {
          entity.coin += price;
          entity.cars.splice(d1.index, 1);
          entity.player.directMessage(
            i18n.t('recycle.success', { lng: entity.language })
          );
        } else {
          return;
        }
      }
    }
  }
  /**
   * 定制赛车
   * @param entity 定制赛车的玩家
   * @returns 无
   */
  static async dz(entity: GamePlayerEntity) {
    await entity.player.dialog<GameTextDialogParams>({
      type: GameDialogType.TEXT,
      title: i18n.t('dz.title', { lng: entity.language }),
      content: i18n.t('dz.content_1', { lng: entity.language }),
    });
    const d1 = await entity.player.dialog<GameInputDialogParams>({
      type: GameDialogType.INPUT,
      title: i18n.t('dz.title', { lng: entity.language }),
      content: i18n.t('dz.content_2', { lng: entity.language }),
    });
    if (!d1) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    if (isNaN(Number(d1))) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    if (Number(d1) < 0 || Number(d1) > 2.5) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    const d2 = await entity.player.dialog<GameInputDialogParams>({
      type: GameDialogType.INPUT,
      title: i18n.t('dz.title', { lng: entity.language }),
      content: i18n.t('dz.content_3', { lng: entity.language }),
    });
    if (!d2) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    if (isNaN(Number(d2))) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    if (Number(d2) < 0 || Number(d2) > 0.6) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    const d3 = await entity.player.dialog<GameInputDialogParams>({
      type: GameDialogType.INPUT,
      title: i18n.t('dz.title', { lng: entity.language }),
      content: i18n.t('dz.content_4', { lng: entity.language }),
    });
    if (!d3) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    if (isNaN(Number(d3))) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    if (Number(d3) < 0 || Number(d3) > 1.5) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    }
    const d5 = await entity.player.dialog<GameInputDialogParams>({
      type: GameDialogType.INPUT,
      title: i18n.t('dz.title', { lng: entity.language }),
      content: i18n.t('dz.name', { lng: entity.language }),
    });
    if (!d5) {
      entity.player.directMessage(
        i18n.t('dz.number_error', { lng: entity.language })
      );
      return;
    } else if (entity.dz.name.includes(d5)) {
      entity.player.directMessage(
        i18n.t('dz.name_error', { lng: entity.language })
      );
      return;
    }
    const price = Number(d1) * 12500 + Number(d2) * 10000 + Number(d3) * 7500;
    const d4 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('dz.title', { lng: entity.language }),
      content: i18n.t('dz.show', {
        lng: entity.language,
        speed: d1,
        acceleration: d2,
        jumpPower: d3,
        need: price,
      }),
      options: [
        i18n.t('common.yes', { lng: entity.language }),
        i18n.t('common.no', { lng: entity.language }),
      ],
    });
    if (d4) {
      if (d4.index === 0) {
        if (entity.coin < price) {
          entity.player.directMessage(
            i18n.t('engineer.buy_fail', { lng: entity.language })
          );
          return;
        }
        entity.player.directMessage(
          i18n.t('dz.success', { lng: entity.language })
        );
        entity.coin -= price;
        entity.dz.name.push(d5);
        entity.dz.speed.push(Number(d1));
        entity.dz.jumpPower.push(Number(d3));
        entity.dz.acceleration.push(Number(d2));
        entity.cars.push({
          id: 'car_22',
          name: d5, //因为玩家可以更改赛车名字，所以名字不使用i18n进行翻译
        });
      } else {
        return;
      }
    }
  }
}
const retrieve = world.querySelector('#回收拆解') as GameEntity;
retrieve.enableInteract = true;
retrieve.interactRadius = 8;
retrieve.onInteract(async ({ entity }) => {
  buyCar.recycle(entity);
});
const cj = world.querySelector('#抽奖') as GameEntity;
cj.enableInteract = true;
cj.interactRadius = 8;
cj.onInteract(async ({ entity }) => {
  const d1 = await entity.player.dialog<GameSelectDialogParams>({
    type: GameDialogType.SELECT,
    title: i18n.t('cj.name'),
    content: `
    ${i18n.t('cj.content', { lng: entity.language })}
    
    ${i18n.t('cj.jc', { lng: entity.language, string: cancj.map((value) => `\n${value.name}`) })}`,
    options: [
      i18n.t('common.yes', { lng: entity.language }),
      i18n.t('common.no', { lng: entity.language }),
    ],
  });
  if (d1) {
    if (d1.index === 0) {
      const buycarClass: buyCar | null = new buyCar();
      buycarClass.cj(entity);
    }
  }
});

const engineer = world.querySelector('#铁匠') as GameEntity;
engineer.enableInteract = true;
engineer.interactRadius = 8;
engineer.onInteract(async ({ entity }) => {
  const d1 = await entity.player.dialog<GameSelectDialogParams>({
    type: GameDialogType.SELECT,
    title: i18n.t('engineer.name'),
    content: i18n.t('engineer.greet', { lng: entity.language }),
    options: [
      i18n.t('engineer.buycar', { lng: entity.language }),
      i18n.t('engineer.dz', { lng: entity.language }),
    ],
  });
  if (!d1) {
    return;
  }
  switch (d1.value) {
    case i18n.t('engineer.change_name', { lng: entity.language }):
      await buyCar.changeCarName(entity);
      break;
    case i18n.t('engineer.buycar', { lng: entity.language }):
      await buyCar.buycar(entity);
      break;
    case i18n.t('engineer.dz', { lng: entity.language }):
      await buyCar.dz(entity);
      break;
  }
});
